package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.SmsLoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/mobile/login")
public class MobileLoginController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private MemberService memberService;

    @PostMapping("/smsLogin")
    public Result Login(@RequestBody SmsLoginParam param , HttpServletRequest request){


        Boolean success = smsService.checkVaildateCode("MOBILE_SIGNIN",param.getTelephone(),param.getValidateCode());
        if (!success) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        Member member = new Member();
        member.setPhoneNumber(param.getTelephone());
        member = memberService.creatMember(member);
        request.getSession().setAttribute("member",member);
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

}
